package cn.hejiang.mamis.core.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hejiang.mamis.core.dto.AmisCrudDTO;
import cn.hejiang.mamis.core.dto.ResultDTO;
import cn.hejiang.mamis.core.dto.SpringSessionDTO;
import cn.hejiang.mamis.core.entity.SpringSession;
import cn.hejiang.mamis.core.mapper.SpringSessionMapper;
import cn.hutool.core.bean.BeanUtil;

@RequestMapping("/mamis/plat/session")
@RestController
public class PlatSessionController {
	@Autowired
	SpringSessionMapper mapper;

	@GetMapping("/list")
	public ResultDTO<AmisCrudDTO<SpringSessionDTO>> getSessionList() {
		final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		QueryWrapper<SpringSession> wrapper = new QueryWrapper<SpringSession>();
		wrapper.lambda().isNotNull(SpringSession::getPrincipalName);
		List<SpringSession> list = mapper.selectList(wrapper);

		List<SpringSessionDTO> dtoList = list.stream().map(session -> {
			SpringSessionDTO dto = BeanUtil.copyProperties(session, SpringSessionDTO.class);

			dto.setCreationTimeStr(df.format(new Date(dto.getCreationTime())));
			dto.setLastAccessTimeStr(df.format(new Date(dto.getLastAccessTime())));
			dto.setExpiryTimeStr(df.format(new Date(dto.getExpiryTime())));

			return dto;
		}).collect(Collectors.toList());

		AmisCrudDTO<SpringSessionDTO> data = new AmisCrudDTO<>();
		data.setItems(dtoList);

		// 返回结果集
		return ResultDTO.success(data);
	}
}
